//
//  GJLongImageController.m
//  CYGJ
//
//  Created by Chen Qian on 2019/4/23.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "GJLongImageController.h"
#import "GJLongImageCell.h"
#import <TYSnapshotScroll.h>

@interface GJLongImageController ()<UICollectionViewDelegateFlowLayout>

@end

@implementation GJLongImageController

static NSString * const reuseIdentifier = @"GJLongImageCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
  self.title = @"拼接预览";
  self.collectionView.backgroundColor = [UIColor whiteColor];
  self.collectionView.showsHorizontalScrollIndicator = false;
  self.collectionView.showsVerticalScrollIndicator = false;
    // Register cell classes
  [self.collectionView registerNib:[UINib nibWithNibName:reuseIdentifier bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    // Do any additional setup after loading the view.
  
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"水平拼接" style:UIBarButtonItemStylePlain target:self action:@selector(toV)];
  
  UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  [saveBtn setFrame:CGRectMake(self.view.bounds.size.width - 70, self.view.bounds.size.height - 90, 50, 50)];
  [saveBtn setTitle:@"完成" forState:UIControlStateNormal];
  [saveBtn.titleLabel setTextColor:[UIColor whiteColor]];
  [saveBtn setBackgroundColor:[UIColor colorWithRed:22/255.0 green:173/255.0 blue:253/255.0 alpha:0.58]];
  [saveBtn addTarget:self action:@selector(toSave) forControlEvents:UIControlEventTouchUpInside];
  saveBtn.layer.masksToBounds = YES;
  saveBtn.layer.cornerRadius = 25;
  [self.view addSubview:saveBtn];
  
}

-(void)viewWillAppear:(BOOL)animated{
  [super viewWillAppear:animated];
  [self.navigationController setNavigationBarHidden:NO animated:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
  [super viewWillDisappear:animated];
  [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void)toV{
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"垂直拼接" style:UIBarButtonItemStylePlain target:self action:@selector(toH)];
  UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
  flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
  [self.collectionView reloadData];
}

-(void)toH{
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"水平拼接" style:UIBarButtonItemStylePlain target:self action:@selector(toV)];
  UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
  flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
  [self.collectionView reloadData];
}

-(void)toSave{
  [TYSnapshotScroll screenSnapshot:self.collectionView finishBlock:^(UIImage *snapShotImage) {
    if (self.toLongDisplay) {
      self.toLongDisplay(snapShotImage);
    }
    [self.navigationController popViewControllerAnimated:NO];
  }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.imageDatas.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GJLongImageCell *cell = (GJLongImageCell*)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    cell.imageCell.image = [self getResultImag:self.imageDatas[indexPath.row]];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
  UIImage *image = [UIImage imageWithContentsOfFile:self.imageDatas[indexPath.row]];
  CGSize resultSize = image.size;
  CGSize deviceSize = self.view.bounds.size;
  deviceSize.height = deviceSize.height - 120;
  
  UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
  if (flowLayout.scrollDirection == UICollectionViewScrollDirectionVertical) {
    resultSize = CGSizeMake(deviceSize.width, deviceSize.width * resultSize.height / resultSize.width);
  }else{
    resultSize = CGSizeMake(deviceSize.height * resultSize.width / resultSize.height, deviceSize.height);
  }
  return resultSize;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
  return 0;
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
  return 0;
}

-(UIImage*)getResultImag:(NSString*)imagePath{
  
  UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
  CGSize resultSize = image.size;
  CGSize deviceSize = self.view.bounds.size;
  deviceSize.height = deviceSize.height - 120;
  BOOL isNeedRender = false;
  
  UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout*)self.collectionView.collectionViewLayout;
  if (flowLayout.scrollDirection == UICollectionViewScrollDirectionVertical) {
    if (resultSize.width > deviceSize.width) {
      isNeedRender = true;
      resultSize = CGSizeMake(deviceSize.width, deviceSize.width * resultSize.height / resultSize.width);
    }
  }else{
    if (resultSize.height > deviceSize.height) {
      isNeedRender = true;
      resultSize = CGSizeMake(deviceSize.height * resultSize.width / resultSize.height, deviceSize.height);
    }
  }

  if (isNeedRender) {
    resultSize = CGSizeMake(deviceSize.width, deviceSize.width * resultSize.height / resultSize.width);
    UIGraphicsBeginImageContextWithOptions(resultSize, YES, 0);
    [image drawInRect:CGRectMake(0, 0, resultSize.width, resultSize.height)];
    image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
  }else{
    return image;
  }
}

@end
